//
//  JPopoverAnimator.swift
//  JMicroBlog
//
//  Created by Jason on 21/5/15.
//  Copyright (c) 2015 Shepherd. All rights reserved.
//

import UIKit

class JPopoverAnimator: NSObject, UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning{
    
    // 是否正在展现
    var isPresentation = false
    
    // 展现视图的大小
    var presentFrame = CGRectZero
    
    /**
    Asks your delegate for the custom presentation controller to use for managing the view hierarchy when presenting a view controller.
    要求代理返回一个自定义的转场控制器,当modal一个视图控制器时,使用该转场控制器来管理视图的层级
    
    :param: presented  要被modal的视图控制器
    :param: presenting 文档说底层视图控制器 实测为nil
    :param: source     ??
    
    :returns: UIPresentationController对象
    */
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController!, sourceViewController source: UIViewController) -> UIPresentationController? {
        let popoverC = JPopoverPresentationC(presentedViewController: presented, presentingViewController: presenting)
        popoverC.presentFrame = presentFrame
        
        return popoverC
    }
    
    /**
    返回model的视图怎样进行动画
    
    :param: presented  被modal的控制器
    :param: presenting 底层控制器
    :param: source     The view controller whose presentViewController:animated:completion: method was called
    
    :returns: The animator object to use when presenting the view controller or nil if you do not want to present the view controller using a custom transition
    // 当你想自定义展现视图控制器的时可以使用一个可选的展现视图控制器的可动画对象
    */
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresentation = true
        
        return self
    }
    
    /**
    返回modal的视图如何消失
    
    :param: dismissed 消失的视图控制器
    
    :returns: 同上
    */
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresentation = false
        
        return self
    }
    
    // MARK: - UIViewControllerAnimatedTransitioning
    /**
    转场动画的时间
    
    :param: transitionContext 转场上下文对象
    
    :returns: 时间
    */
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
        return 0.5
    }
    
    /**
    转场动画
    
    :param: transitionContext 转场上下文,提供转场动画的相关信息 从哪来,到哪去
    // 用于主动提供转场动画,一旦实现了此方法,系统的动画将会失效
    */
    func animateTransition(transitionContext: UIViewControllerContextTransitioning){
        if isPresentation == true {
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
            // 将toVC的view添加到容器视图
            transitionContext.containerView().addSubview(toView)
            
            // 动画方法 高度为0
            toView.transform = CGAffineTransformMakeScale(1.0, 0.0)
            // 设置动画图层的锚点
            toView.layer.anchorPoint = CGPointMake(0.5, 0)
            
            // 设置动画
            UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 5.0, options: nil, animations: { () -> Void in
                toView.transform = CGAffineTransformMakeScale(1.0, 1.0)
                }, completion: { _ in
                    // 一定要执行的函数 告诉系统转场动画已经完成
                    transitionContext.completeTransition(true)
            })
            
        }else {
            
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
            // 从父视图中移除
            fromView?.removeFromSuperview()
            // 记着通知系统转场动画已经完成
            transitionContext.completeTransition(true)
        }
    }

}
